package leetcode_1101_1200;

public class LeeCode_1156 {
    public static void main(String[] args) {

    }
    private static int maxRepOpt1(String text) {
        int[] tab = new int[26];
        char[] chars = text.toCharArray();
        int n = text.length();
        for (int i = 0; i < n; i++) {
            tab[chars[i] - 'a']++;
        }
        int i = 1, ans = 1, count = 1;
        while (i < n){
            if (chars[i] == chars[i - 1]){
                count++;
            }else {
                if (count < tab[chars[i - 1] - 'a']){
                    ans = Math.max(count + 1, ans);
                }else {
                    ans = Math.max(count, ans);
                }
                count = 1;
            }
            i++;
        }
        if (count < tab[chars[i - 1] - 'a']){
            ans = Math.max(count + 1, ans);
        }else {
            ans = Math.max(count, ans);
        }

        for (i = 1; i < n - 1; i++) {
            if (chars[i] != chars[i - 1] && chars[i - 1] == chars[i + 1]){
                char c = chars[i - 1];
                int c1 = 0, c2 = 0, l = i - 1, r = i + 1;
                while (l >= 0 && chars[l--] == c){
                    c1++;
                }
                while (r < n && chars[r++] == c){
                    c2++;
                }
                int sum = c1 + c2;
                if (sum < tab[c - 'a']){
                    ans = Math.max(sum + 1, ans);
                }else {
                    ans = Math.max(sum, ans);
                }
            }
        }
        return ans;
    }
}
